package com.svms.vehicle;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

import com.svms.service.constants.Constants;
import com.svms.service.vo.VehicleVO;

public class VehicleListModelItemRender implements ListitemRenderer {

	@Override
	public void render(Listitem item, Object data, int index) throws Exception {

		final VehicleVO vehicle = (VehicleVO) data;

		Listcell lc = new Listcell(vehicle.getVehicleId());
		lc.setParent(item);
		lc = new Listcell(vehicle.getVehicleNumber());
		lc.setParent(item);
		lc = new Listcell(vehicle.getModel());
		lc.setParent(item);

		if (vehicle.getTransport() != null) {
			lc = new Listcell(vehicle.getTransport().getTransportId());
			lc.setParent(item);
		}

		Map<String, Date> vehicleDocs = vehicle.getVehicleDocuments();

		if (vehicleDocs != null) {
			lc = new Listcell(formatDate(vehicle.getVehicleDocuments().get(
					Constants.FORM47_TD)));
			lc.setParent(item);
			lc = new Listcell(formatDate(vehicle.getVehicleDocuments().get(
					Constants.NC_TD)));
			lc.setParent(item);
			lc = new Listcell(formatDate(vehicle.getVehicleDocuments().get(
					Constants.FC_TD)));
			lc.setParent(item);
			lc = new Listcell(formatDate(vehicle.getVehicleDocuments().get(
					Constants.INSURANCE_TD)));
			lc.setParent(item);
			lc = new Listcell(formatDate(vehicle.getVehicleDocuments().get(
					Constants.TAX_TD)));
			lc.setParent(item);
		}

		item.setAttribute("data", data);

		ComponentsCtrl.applyForward(item,
				"onDoubleClick=onVehicleItemDoubleClicked");

	}

	private String formatDate(Date date) {
		String formatedDate = null;
		if (date != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
			formatedDate = dateFormat.format(date);
		}
		return formatedDate;
	}
}
